package org.tangerinejunction.imstats.ui;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

public class Preferences extends Properties 
{
	public static final String FILE_NAME = "wednesday.properties";
	
	public static final String LABEL_FULL_NAME = "name";
	public static final String LABEL_SHORT_NAME = "short name";
	public static final String LABEL_ONE_CHAR = "single char";
	public static final String LABEL_ORDINALITY = "ordinality";
	
	public static final String LABEL_OPTION = "label option";
	public static final String LIMIT_TOOLTIP_LABEL = "limit tooltip label";
	public static final String NUM_LABEL_CHARS = "number of label characters";
	
	private static Preferences instance;
	
	private Preferences()
	{
		super();
		try {
			load(new FileReader(FILE_NAME));
		} catch (FileNotFoundException e) {
			setDefaults();
		} catch (IOException e) {
			// TODO: show the user a message?
			e.printStackTrace();
			setDefaults();
		}
	}
	
	private void setDefaults()
	{
		setProperty(LABEL_OPTION, LABEL_FULL_NAME);
		setProperty(LIMIT_TOOLTIP_LABEL, Boolean.toString(false));
		setProperty(NUM_LABEL_CHARS, Integer.toString(8));
	}
	
	public synchronized static Preferences getInstance()
	{
		if (instance == null)
			instance = new Preferences();
		return instance;
	}
	
	public String getLabelOption()
	{
		return getProperty(LABEL_OPTION);
	}
	
	public void setLabelOption(String labelOption)
	{
		setProperty(LABEL_OPTION, labelOption);
	}
	
	public boolean isLimitTooltipLabel()
	{
		return Boolean.parseBoolean(getProperty(LIMIT_TOOLTIP_LABEL));
	}
	
	public void setLimitTooltipLabel(boolean limitTooltipLabel) {
		setProperty(LIMIT_TOOLTIP_LABEL, Boolean.toString(limitTooltipLabel));
	}
	
	public int getNumLabelChars()
	{
		return Integer.parseInt(getProperty(NUM_LABEL_CHARS));
	}
	
	public void setNumLabelChars(int numLabelChars)
	{
		setProperty(NUM_LABEL_CHARS, Integer.toString(numLabelChars));
	}

	public void saveProperties()
	{
		try {
			store(new FileWriter(FILE_NAME), "");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
